package com.mathdroid.gui.main.character;

import java.util.LinkedList;

import javax.swing.tree.DefaultMutableTreeNode;

import com.mathdroid.model.Model;
import com.mathdroid.model.enums.EItemStat;
import com.mathdroid.model.items.EquipedItems;
import com.mathdroid.model.stats.ACharacterStats;
import com.mathdroid.model.stats.CharacterStatsFactory;

/**
 * This tree model contains information about the profile and character.
 */
public final class DroidTreeModel extends DefaultMutableTreeNode {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Create the tree model with profile and character information.
	 */
	public DroidTreeModel() {
		super("Profile: " + Model.getProfile().getName());
		
		this.addCharacterStats();
		this.addItemTemplate();
		this.addSpec();
		this.addEncounter();
		this.addBuffTemplate();
		this.addAbilityTemplate();
	}
	
	/**
	 * Add information about all (total) character stats.
	 */
	private void addCharacterStats() {
		DefaultMutableTreeNode charStats = new DefaultMutableTreeNode(
				"Character Stats");
		this.add(charStats);
		
		DefaultMutableTreeNode itemAndBaseStats = new DefaultMutableTreeNode(
				"Item Stats plus Base Stats");
		charStats.add(itemAndBaseStats);
		
		ACharacterStats s = new CharacterStatsFactory().get(Model.getESpec());
		
		/* Add all character stats. */
		for (EItemStat estat : EItemStat.getAllStats()) {
			if (s.getTotalStats(estat) > 0) {
				DefaultMutableTreeNode n = new DefaultMutableTreeNode(
						estat.getName() + ": " + s.getTotalStats(estat)
				);
				itemAndBaseStats.add(n);
			}
		}
		
		/* Add specialization specific stats. */
		DefaultMutableTreeNode resultingStats = new DefaultMutableTreeNode(
				"Calculated Stats");
		charStats.add(resultingStats);
		
		for (LinkedList<String> list : s.getAllDataLists()) {
			
			DefaultMutableTreeNode title = 
				new DefaultMutableTreeNode(list.get(0));
			resultingStats.add(title);
			
			for (int i = 1; i < list.size(); i++) {
				DefaultMutableTreeNode node = 
					new DefaultMutableTreeNode(list.get(i));
				title.add(node);
			}
		}
	}

	/**
	 * Add information about the ability template.
	 */
	private void addAbilityTemplate() {
		DefaultMutableTreeNode templateName = new DefaultMutableTreeNode(
				"Ability Template: " 
				+ Model.getProfile().getAbilities().toString());
		this.add(templateName);
	}
	
	/**
	 * Add information about the specialization and talents.
	 */
	private void addSpec() {
		DefaultMutableTreeNode specName = new DefaultMutableTreeNode(
				"Specialization: " + Model.getESpec());
		this.add(specName);
		
		DefaultMutableTreeNode talents = new DefaultMutableTreeNode(
				"Template: " + Model.getProfile().getSpecTemplate().toString());
		specName.add(talents);
	}
	
	/**
	 * Add information about the encounter settings.
	 */
	private void addEncounter() {
		DefaultMutableTreeNode encounterTemplate = new DefaultMutableTreeNode(
				"Encounter: " + Model.getProfile().getEncounter().toString());
		this.add(encounterTemplate);
		
		DefaultMutableTreeNode encounterDuration = new DefaultMutableTreeNode(
				"Duration: " + Model.getProfile().getEncounter().getMinutes() 
				+ "min " +  Model.getProfile().getEncounter().getSeconds() 
				+ "sec");
		encounterTemplate.add(encounterDuration);
	}
	
	/**
	 * Add information about the used item template.
	 */
	private void addItemTemplate() {
		DefaultMutableTreeNode itemTemplate = new DefaultMutableTreeNode(
				"Item Template: " + Model.getProfile().getEquipedItems());
		this.add(itemTemplate);
		
		EquipedItems ei = Model.getProfile().getEquipedItems();
		
		for (EItemStat estat : EItemStat.getAllStats()) {
			if (ei.getTotalStats(estat) > 0) {
				DefaultMutableTreeNode n = new DefaultMutableTreeNode(
						estat.getName() + ": " + ei.getTotalStats(estat)
				);
				itemTemplate.add(n);
			}
		}
	}
	
	/**
	 * Add information about the used buff template.
	 */
	private void addBuffTemplate() {
		DefaultMutableTreeNode buffTemplate = new DefaultMutableTreeNode(
				"Buff Template: " + Model.getProfile().getBuffTemplate());
		this.add(buffTemplate);
	}
}
